<?php
class Clscandir{
    public $path;
	public $completeFiles ;
	public $completeDirs ;
	public $files;
	public $dirs ;
	public $filesDirs ;
	public $completeFilesDirs ;
	
	//把文件转化为文件所在目录字串
	//eg: I:/AppServ/www/_App_/inc/baseLib.php
    //变为 I:/AppServ/www/_App_/inc/
	public static function fileToDirname($path){
	    if( is_file($path) ){
		    return dirname($path);	
		} 
		return $path;
	}
    public function  __construct($path="I:/_phplib/",$sortingOrder=0){
		$path = Clscandir::fileToDirname($path);
		$this->path = $path ;
		$this->scandir($path , $sortingOrder);
		$this->getCompleteFilesDirs();
		$this->getCompleteDirs();
		return $this->filesDirs;
	}
	//  第一步
	public function  scandir($path='',$sortingOrder=0){
		if(empty($path)){
		    $path=$this->path;	
		}
		//获取目录下的所有文件s和文件夹s  scandir可用dir替代
 		$this->filesDirs=scandir ( $path , $sortingOrder ) ;  
		return $this->filesDirs;       
		//sort()升序  rsort()降序 ;
	}
	
	//得到完整路径的目录和文件
	public function getCompleteFilesDirs(){
		if(count($this->completeFilesDirs)==0){
			foreach($this->filesDirs as $k => $v ){
				$this->completeFilesDirs[] = self::formatPath($this->path."/".$v);   	
			}
		}
		return $this->completeFilesDirs;
		
	} 
	public function getFiles($regexp=false){
		$fils=array();
	        foreach($this->filesDirs as $k => $v ){
				if(!is_dir($this->path."/".$v)  ){
					 
					$fils[] =$v ; 

				}
			}	
		return $fils;
	}
	//排除 .html文件
	//getFiles_nomatch( "/(\.html)$/");
	public function getFiles_nomatch($regexp=false){
		$fils=array();
	        foreach($this->filesDirs as $k => $v ){
				if(!is_dir($this->path."/".$v)  ){
					if(!$regexp){
					$fils[] =$v ; 
					}else{
						if(!preg_match($regexp,$v)){
							$fils[] =$v ; 
						}	
					}
				}
			}	
		return $fils;
	}
	public function getFiles_match($regexp=false){
		$fils=array();
	        foreach($this->filesDirs as $k => $v ){
				if(!is_dir($this->path."/".$v)  ){
					if(!$regexp){
					$fils[] =$v ; 
					}else{
						if(preg_match($regexp,$v)){
							$fils[] =$v ; 
						}	
					}
				}
			}	
		return $fils;
	}
	public function getDirs(){
		$dirs=array();
	        foreach($this->filesDirs as $k => $v ){
				if(is_dir($this->path."/".$v))
				$dirs[] =$v ;   	
			}	
		return $dirs;
	}
	
	//得到完整的目录文件名
	public function getCompleteDirs(){

		if(count($this->completeDirs)==0){
			foreach($this->completeFilesDirs as $k=>$v){			
				if(is_dir($v)){
					$this->completeDirs[]=$v;
				}else{
					$this->completeFiles[]=$v;	
				}	
			}
		}
		return $this->completeDirs;
		
	}
	public function getCompleteFiles(){
		return $this->completeFiles;
	}	
    
	//把文件数组变为网页的href地址
	public static function toUrl($arr,$target='_blank'){
	    $ret='';
		foreach($arr as $v){
		    $ret.= '<li><a href="'.$v.'?r='.rand(1,9999999).'" title="'.$v.'" target="'.$target.'">'
			.preg_replace('/(.{0,}?)([0-9a-zA-Z\-_\.\$]{0,})(html|php|htm|txt)$/','\\1\\2\\3',$v)
			.'</a></li>';	
 	
		}	
		return $ret;
	}

	public static function formatPath($str){		
	    return preg_replace (     '/\/{1,}/' , '/' ,
	           str_replace ( '\\' , '/' , $str )   ) ;	
    }
	

	public static function toHttpUrl($arr,
	$middle_path="/_doll/_ae/"){
		$severpath="http://".$_SERVER['SERVER_NAME']
		.":".$_SERVER['SERVER_PORT']
		.$middle_path;
        $retarr=array();
		foreach($arr as $k=>$v){
			 $v=preg_replace(
	  "/[\d\D]*\/([a-zA-Z0-9_\.\-\[\]\(\)]*\.(?:flv|f4v|mp[4]|mkv))$/"
       ,$severpath."\\1",$v);
	       if(!preg_match("/^[a-zA-Z]\:/",$v))
		   {
			   $retarr[$k]=Clscandir::replacePoundsign($v); 	
		   }	
		}
		return $retarr;		
	}
	public static function replacePoundsign($str){
        $str=iconv('GB2312','UTF-8', $str)  ;
	    $str= preg_replace("/#/","%23",$str);
		return $str;
	}	
	public  function getHttpUrls($wwwPath,$arr=''){
		
		if( empty($arr) ){
			$arr = $this->getCompleteFiles() ;
		}
		
		$retarr=array();
		foreach($arr as $k=>$v){
			$retarr[]=self::filetourl($wwwPath,$v);
		}
		return $retarr;
	}
	public static function filetourl($wwwPath,$filename,$s=''){
		
		$server_url="http:".$s."//".$_SERVER['SERVER_NAME'].":".$_SERVER['SERVER_PORT']."/";
		return str_replace(array($wwwPath,"#"),array($server_url,"%23"),$filename);
		
	}
}
?>